//
//  DISgoodsDetailViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/13.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "DISgoodsDetailViewController.h"
#import "XSQBanner.h"
#import "DISgoodsDetaileModel.h"
#import "CSPersonModel.h"
#import "HFAddAddressViewController.h"
#import "AmakeAccViewController.h"

@interface DISgoodsDetailViewController ()<UIScrollViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong) XSQBanner *banner;
@property (nonatomic,assign) BOOL isrefa;
@property(nonatomic,strong)DISgoodsDetaileModel *modlel;
@property(nonatomic,strong)NSMutableArray *commitArr;
@property(nonatomic,strong)NSMutableArray *imageArr;
@property(nonatomic,strong)UIView *buyView;
@property(nonatomic,strong)UILabel *buyViewprice;
@property(nonatomic,strong)UILabel *buyViewtitle;
@property(nonatomic,strong)UILabel *buyViewnumber;
@property(nonatomic,strong)UITextField *numberTF;
@property(nonatomic,strong)UIImageView *buyViewimage;
@property(nonatomic,strong)CSPersonModel *addressmodel;

@end

@implementation DISgoodsDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"产品详情";
    self.isrefa  = NO;
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.commitArr = [NSMutableArray arrayWithCapacity:1];
    self.imageArr = [NSMutableArray arrayWithCapacity:1];
    self.modlel =[[DISgoodsDetaileModel alloc]init];
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    self.addressmodel = [[CSPersonModel alloc]init];
    [self requestDetaile];
    [self requestaddress];
    
    // 添加通知监听见键盘弹出/退出
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardAction:) name:UIKeyboardWillHideNotification object:nil];
    // Do any additional setup after loading the view.
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [_buyViewimage removeFromSuperview];
}
// 键盘监听事件
- (void)keyboardAction:(NSNotification*)sender{
    // 通过通知对象获取键盘frame: [value CGRectValue]
    NSDictionary *useInfo = [sender userInfo];
    NSValue *value = [useInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    
    // <注意>具有约束的控件通过改变约束值进行frame的改变处理
    if([sender.name isEqualToString:UIKeyboardWillShowNotification]){
        self.buyView.y = -[value CGRectValue].size.height+150;
    }else{
        self.buyView.y= 0;
    }
}
-(void)requestDetaile{
    
    NSMutableDictionary *mudic = [NSMutableDictionary dictionaryWithCapacity:1];
    mudic[@"id"] = NOTNIL(_goodsID);
    kWeakSelf;
    [NetWorkTool POST:platProInfo param:mudic success:^(id dic) {
        KMyLog(@"++++++++++%@",[HFTools toJSONString:dic]);
        [DYModelMaker DY_makeModelWithDictionary:[dic objectForKey:@"data"] modelKeyword:@"" modelName:@""];
        weakSelf.modlel = [DISgoodsDetaileModel mj_objectWithKeyValues:[dic objectForKey:@"data"]];
        [self showdetaile];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:NO];
}
-(void)showdetaile{
    [self.view addSubview:self.scrollView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //btn.frame = CGRectMake(0, KHEIGHT-44, KWIDTH, 44);
    btn.titleLabel.font = FontSize(16);
    btn.backgroundColor = zhutiColor;
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"立即订货" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buyAddCart:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(44);
    }];
    
    //创建产品详情
    [self.scrollView addSubview:self.banner];
    
    NSMutableArray *muarr = [NSMutableArray arrayWithCapacity:1];
    
    for (ImgsModel *model in _modlel.imgs) {
        [muarr addObject:model.image_url];
    }
    
    [_banner updateImageArr:muarr AndImageClickBlock:^(NSInteger index) {
        
    }];
    
    UILabel *bglable = [[UILabel alloc]initWithFrame:CGRectMake(0, _banner.bottom, KWIDTH, 105)];
    bglable.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:bglable];
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(16, _banner.bottom+10, KWIDTH-32, 40)];
    name.textColor = KTEXTCOLOR;
    name.font = FontSize(18);
    name.text = _modlel.parts_name;
    name.numberOfLines = 2;
    //[name sizeToFit];
    [self.scrollView addSubview:name];
    
    UILabel *priceleft = [[UILabel alloc]initWithFrame:CGRectMake(16, name.bottom+15, 45, 20)];
    priceleft.textColor = KTEXTCOLOR;
    priceleft.font = FontSize(14);
    priceleft.text =[NSString stringWithFormat:@"指导价"];
    [self.scrollView addSubview:priceleft];
    
    UILabel *price = [[UILabel alloc]initWithFrame:CGRectMake(63, name.bottom+15, 200, 20)];
    price.textColor = zhutiColor;
    price.font = FontSize(18);
    price.text =[NSString stringWithFormat:@"¥%@",_modlel.parts_price];
    [self.scrollView addSubview:price];
    //
    UILabel *pingtaiLB = [[UILabel alloc]initWithFrame:CGRectMake(KWIDTH-140, name.bottom+15, 120, 20)];
    pingtaiLB.textColor = [UIColor colorWithHexString:@"#FEC351"];
    pingtaiLB.textAlignment = NSTextAlignmentCenter;
    pingtaiLB.font = FontSize(13);
    pingtaiLB.text =[NSString stringWithFormat:@"平台直接提供保障"];
    pingtaiLB.backgroundColor = [UIColor colorWithHexString:@"#FFF2E8D5"];
    
    UIImageView *pictext = [[UIImageView alloc]initWithFrame:CGRectMake((KWIDTH-305)/2, bglable.bottom+25, 305, 16)];
    pictext.image = imgname(@"picText");
    [self.scrollView addSubview:pictext];
    
    for (int i = 0; i < _modlel.imgs.count; i++) {
        ImgsModel *model =_modlel.imgs[i];
        UIImageView *showImage = [[UIImageView alloc]initWithFrame:CGRectMake(0,pictext.bottom+16+ i*KWIDTH, KWIDTH, KWIDTH)];
        if ([model.image_url hasPrefix:@"http"]) {
            [showImage sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:defaultImg];
        }else{
            sdimg(showImage, model.image_url);
        }
        [self.scrollView addSubview:showImage];
        self.scrollView.contentSize = CGSizeMake(KWIDTH,showImage.bottom+20);
    }
}
- (void)buyAddCart:(UIButton *)but{
    //立即订货
    [[UIApplication sharedApplication].keyWindow addSubview:self.buyView];
}
- (XSQBanner *)banner {
    if (!_banner) {
        _banner = [[XSQBanner alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KWIDTH)];
        _banner.pageLocation = PageControlLocationCenter;
    }
    return _banner;
}
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT-44-kNaviHeight)];
        _scrollView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
        _scrollView.delegate = self;
        adjustInset(_scrollView);
    }
    return _scrollView;
}
-(UIView *)buyView{
    if (!_buyView) {
        _buyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
        _buyView.backgroundColor = RGBA(0, 0, 0, 0.5);
        UIView *whiteview = [[UIView alloc]initWithFrame:CGRectMake(0, KHEIGHT-344, KWIDTH, 344)];
        [_buyView addSubview:whiteview];
        
        whiteview.backgroundColor = [UIColor whiteColor];
        UIButton *quxioaBUt = [UIButton buttonWithType:(UIButtonTypeCustom)];
        quxioaBUt.frame = CGRectMake(KWIDTH-55, 8, 40, 22);
        [quxioaBUt setTitle:@"取消" forState:(UIControlStateNormal)];
        [quxioaBUt setTitleColor:[UIColor colorWithHexString:@"#979797"] forState:(UIControlStateNormal)];
        [whiteview addSubview:quxioaBUt];
        [quxioaBUt addTarget:self action:@selector(quxiaoAction:) forControlEvents:(UIControlEventTouchUpInside)];
        
        self.buyViewimage = [[UIImageView alloc]initWithFrame:CGRectMake(16, 48, 90, 90)];
        
        [whiteview addSubview:_buyViewimage];
        
        if ([_modlel.image_url hasPrefix:@"http"]) {
            [_buyViewimage sd_setImageWithURL:[NSURL URLWithString:_modlel.image_url] placeholderImage:defaultImg];
        } else {
            sdimg(_buyViewimage, _modlel.image_url);
        }
        
        self.buyViewprice = [[UILabel alloc]initWithFrame:CGRectMake(118, 54, 221, 16)];
        self.buyViewprice.font = FontSize(16);
        self.buyViewprice.textColor = redPriceColor;
        [whiteview addSubview:_buyViewprice];
        self.buyViewprice.text  = [NSString stringWithFormat:@"¥ %@",_modlel.parts_price];
        
        self.buyViewtitle = [[UILabel alloc]initWithFrame:CGRectMake(118,  self.buyViewprice.bottom + 10, KWIDTH-87-16, 30)];
        self.buyViewtitle.font = FontSize(12);
        self.buyViewtitle.textColor = K333333;
        [whiteview addSubview:_buyViewtitle];
        self.buyViewtitle.text  = [NSString stringWithFormat:@"%@",_modlel.parts_name];
        
        self.buyViewnumber = [[UILabel alloc]initWithFrame:CGRectMake(118,  self.buyViewtitle.bottom + 14, KWIDTH-118-16, 14)];
        self.buyViewnumber.font = FontSize(14);
        self.buyViewnumber.textColor = K999999;
        [whiteview addSubview:_buyViewnumber];
        self.buyViewnumber.text  = [NSString stringWithFormat:@"已选择:   1个"];
        
        UILabel *loin = [[UILabel alloc]initWithFrame:CGRectMake(16, _buyViewnumber.bottom+10, KWIDTH-32, 1)];
        loin.backgroundColor = [UIColor colorWithHexString:@"#FFF2F2F2"];
        [whiteview addSubview:loin];
        
        [whiteview addSubview:_buyViewnumber];
        
        UILabel *xiadanshuliang  = [[UILabel alloc]initWithFrame:CGRectMake(16,  loin.bottom+23, 60, 26)];
        xiadanshuliang.font = FontSize(14);
        xiadanshuliang.text = @"购买数量";
        xiadanshuliang.textColor = K333333;
        [whiteview addSubview:xiadanshuliang];
        
        UIView *bigview = [[UIView alloc]initWithFrame:CGRectMake(KWIDTH-31-116, loin.bottom+22, 116, 24)];
        bigview.layer.masksToBounds = YES;
        bigview.layer.borderWidth = 1;
        bigview.layer.borderColor = K333333.CGColor;
        [whiteview addSubview:bigview];
        
        UIButton *jianhao = [UIButton buttonWithType:(UIButtonTypeCustom)];
        jianhao.frame = CGRectMake(0, 0, 32, 24);
        [jianhao setTitle:@"-" forState:(UIControlStateNormal)];
        [jianhao setTitleColor:K999999 forState:(UIControlStateNormal)];
        [jianhao addTarget:self action:@selector(jianhaoaction:) forControlEvents:(UIControlEventTouchUpInside)];
        [bigview addSubview:jianhao];
        
        UILabel *shuLB = [[UILabel alloc]initWithFrame:CGRectMake(32, 0, 1, 24)];
        shuLB.backgroundColor = K333333;
        [bigview addSubview:shuLB];
        
        UIButton *jiahao = [UIButton buttonWithType:(UIButtonTypeCustom)];
        jiahao.frame = CGRectMake(84, 0, 32, 24);
        [jiahao setTitle:@"+" forState:(UIControlStateNormal)];
        [jiahao addTarget:self action:@selector(jiahaoaction:) forControlEvents:(UIControlEventTouchUpInside)];
        [jiahao setTitleColor:K999999 forState:(UIControlStateNormal)];
        
        [bigview addSubview:jiahao];
        UILabel *shuLB2 = [[UILabel alloc]initWithFrame:CGRectMake(83, 0, 1, 24)];
        shuLB2.backgroundColor = K333333;
        [bigview addSubview:shuLB2];
        
        self.numberTF = [[UITextField alloc]initWithFrame:CGRectMake(33, 0, 50, 24)];
        self.numberTF.delegate = self;
        [bigview addSubview: self.numberTF];
        self.numberTF.text = @"1";
        self.numberTF.font = FontSize(14);
        self.numberTF.textAlignment = NSTextAlignmentCenter;
        self.numberTF.textColor = [UIColor colorWithHexString:@"#979797"];
        self.numberTF.keyboardType = UIKeyboardTypeASCIICapableNumberPad;
        [bigview addSubview:_numberTF];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 344-44, KWIDTH, 44);
        btn.titleLabel.font = FontSize(16);
        btn.backgroundColor = zhutiColor;
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitle:@"立即订货" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(buyformebuyview:) forControlEvents:UIControlEventTouchUpInside];
        [whiteview addSubview:btn];
    }
    return _buyView;
}
-(void)buyformebuyview:(UIButton *)but{
    [_buyView removeFromSuperview];
    NSInteger ind = [_numberTF.text integerValue];
    if (ind<1) {
        ShowToastWithText(@"请输入有效产品数量");
        return;
    }
    
    self.modlel.mycoun = [NSString stringWithFormat:@"%ld",ind];
    
    AmakeAccViewController *acc = [[AmakeAccViewController alloc]init];
    acc.model = _modlel;
    [self.navigationController pushViewController:acc animated:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.numberTF.text = [NSString stringWithFormat:@"%@",textField.text];
    self.buyViewnumber.text  = [NSString stringWithFormat:@"已选择:   %@个",textField.text];
}
-(void)jianhaoaction:(UIButton *)but{
    //减号
    NSInteger index = [self.numberTF.text integerValue];
    index -=1;
    if (index == 0) {
        index = 1;
    }
    self.numberTF.text = [NSString stringWithFormat:@"%ld",index];
    self.buyViewnumber.text  = [NSString stringWithFormat:@"已选择:   %ld个",index];
}
-(void)jiahaoaction:(UIButton *)but{
    NSInteger index = [self.numberTF.text integerValue];
    index +=1;
    self.numberTF.text = [NSString stringWithFormat:@"%ld",index];
    self.buyViewnumber.text  = [NSString stringWithFormat:@"已选择:   %ld个",index];
}
-(void)quxiaoAction:(UIButton *)but{
    [_buyView removeFromSuperview];
}
-(void)requestaddress{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:CcentenInfo param:nil success:^(id dic) {
        [self.addressmodel setValuesForKeysWithDictionary:[dic objectForKey:@"data"]];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}
@end
